package io.p2p.stream.turn;

import io.p2p.stream.turn.model.TurnSharedModel;
import io.p2p.stream.turn.service.TurnUdpServer;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class TurnServiceStarter {
    public static void main(String[] args) {
        CountDownLatch serverLatch = new CountDownLatch(1);

        TurnUdpServer firstTurnUdpServer = new TurnUdpServer(TurnSharedModel.turnServerName, TurnSharedModel.turnServerFirstIP, TurnSharedModel.turnServerFirstPort);
        firstTurnUdpServer.start(serverLatch);

        // 注册 JVM 关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("Shutting down...");
            try {
                serverLatch.await();  // 等待所有服务关闭
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            log.info("Shutdown complete.");
        }));

        try {
            // 主线程等待
            serverLatch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}